package com.starblink.android.basic.util

import android.graphics.Typeface
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import com.starblink.basic.style.R
import com.starblink.basic.util.CommUtils

/**
 *function: waiting for add
 *
 *@author zy
 *@since 2021/6/7
 */
object FontUtil {
    val DM_SANS_BOLD = R.font.dm_sans_bold
    val DM_SANS_MEDIUM = R.font.dm_sans_medium
    val DM_SANS_REGULA = R.font.dm_sans_regular
    val KLARNA = R.font.dm_sans_bold
    val MONTSERRAT_MEDIUM = R.font.montserrat_medium
    val MONTSERRAT_BOLD = R.font.montserrat_bold
    val MONTSERRAT_SEMIBOLD = R.font.montserrat_semibold

    /**缓存字体 Map*/
    private val cacheTypeFaceMap: HashMap<Int, Typeface> = HashMap()


    /**
     * 设置 DM_SANS_BOLD 字体
     */
    fun setSansRegula(mTextView: TextView) {
        try {
            mTextView.typeface = getTypeface(DM_SANS_REGULA)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    /**
     * 设置 DM_SANS_BOLD 字体
     */
    fun setSansBold(mTextView: TextView) {
        try {
            mTextView.typeface = getTypeface(DM_SANS_BOLD)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 设置 DM_SANS_MEDIUM 字体
     */
    fun setSansMedium(mTextView: TextView) {
        try {
            mTextView.typeface = getTypeface(DM_SANS_MEDIUM)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 设置 DM_MONTSERRAT_MEDIUM 字体
     */
    fun setMontseratRegula(mTextView: TextView) {
        try {
            mTextView.typeface = getTypeface(MONTSERRAT_MEDIUM)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    /**
     * 设置 DM_MONTSERRAT_BOLD 字体
     */
    fun setMontseratBold(mTextView: TextView) {
        try {
            mTextView.typeface = getTypeface(MONTSERRAT_BOLD)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 设置 DM_MONTSERRAT_BOLD 字体
     */
    fun setMontseratSemiBold(mTextView: TextView) {
        try {
            mTextView.typeface = getTypeface(MONTSERRAT_SEMIBOLD)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 设置 DM_MONTSERRAT_MEDIUM 字体
     */
    fun setMontseratMedium(mTextView: TextView) {
        try {
            mTextView.typeface = getTypeface(MONTSERRAT_MEDIUM)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    /**
     * 设置 Klarna 字体
     */
    fun setKlarna(mTextView: TextView) {
        try {
            mTextView.typeface = getTypeface(KLARNA)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    /**
     * 获取字体 Typeface 对象
     */
    fun getTypeface(fontResId: Int): Typeface? {
        val cacheTypeface = cacheTypeFaceMap[fontResId]
        if (cacheTypeface != null) {
            return cacheTypeface
        }
        return try {
            val typeface: Typeface? = ResourcesCompat.getFont(CommUtils.getContext(), fontResId)
            cacheTypeFaceMap[fontResId] = typeface!!
            typeface
        } catch (e: Exception) {
            e.printStackTrace()
            Typeface.DEFAULT
        }
    }
}